package graphic.dialogs;

import graphic.FilesTabbedPanel;
import graphic.MainFrame;
import graphic.mycomponents.TextChecked;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import utility.GraphicUtilsFactory;
import core.Configuration;
import dbms.DBCheckAndConfigurationFunctions;
import dbms.DBParameters;
import dbms.DBSelectingFunctions;

public class ProgramConfigurationDialog extends AbstractDialog 
{	
	String programName;
	
	JPanel radioButtonPanel;
	ButtonGroup programType;
	JRadioButton executable;
	JRadioButton java;
	
	TextChecked programExecutingName;
	TextChecked programDir;
	
	public ProgramConfigurationDialog()
	{	
		super("Program Configuration", "Configure", 300, 420);		
	}
	
	@Override
	public void createPanel() 
	{
		super.setPanel(new JPanel(new GridBagLayout()));		
		JPanel panel = super.getPanel(); 
		
		this.createFields();
		
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5, 10, 5, 10);
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.CENTER;
		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		panel.add(new JLabel("Program Configuration"), c);
		
		c.gridx = 0;
		c.gridy = 1;
		panel.add(this.programExecutingName, c);	
	
		c.gridx = 0;
		c.gridy = 2;
		panel.add(this.programDir, c);
		
		c.gridx = 0;
		c.gridy = 3;
		panel.add(this.radioButtonPanel, c);
		
		c.gridx = 0;
		c.gridy = 4;
		c.gridwidth = 1;
		panel.add(super.getOkButton(), c);	
	
		c.gridx = 1;
		c.gridy = 4;
		panel.add(super.getExitButton(), c);	
	}

	public void createFields()
	{
		this.programName = FilesTabbedPanel.getIstance().getSelectedProgramName();
		
		super.setTitle("Program Configuration:" + this.programName);
		
		this.programType = new ButtonGroup();
		this.executable = new JRadioButton("Executable");
		this.java = new JRadioButton("Java Program");
		
		this.radioButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		this.radioButtonPanel.add(this.executable);
		this.radioButtonPanel.add(this.java);
		
		this.programType.add(executable);
		this.programType.add(java);

		String programDir = "";
		String executingName = "";
		
		if(DBCheckAndConfigurationFunctions.isProgramConfigured(this.programName))
		{
			try {
				programDir = DBSelectingFunctions.getProgramParameter(this.programName, DBParameters.PROGRAMDIR);
				executingName = DBSelectingFunctions.getProgramParameter(this.programName, DBParameters.PROGRAMEXECUTINGNAME);
				
				if(DBSelectingFunctions.getProgramParameter(this.programName, DBParameters.PROGRAMTYPE).equals(DBParameters.EXECUTABLE))
					this.executable.getModel().setSelected(true);
				else
					this.java.getModel().setSelected(true);
			} catch (SQLException e) {}
		}
			
		this.programExecutingName = new TextChecked(executingName, TextChecked.TEXT, true, false, null);
		this.programDir = new TextChecked(programDir, TextChecked.FOLDER, true, true, null);
		
		GraphicUtilsFactory.setLobbyTextFieldParameters(this.programExecutingName, "Program Executing Name");
		GraphicUtilsFactory.setLobbyTextFieldParameters(this.programDir, "Program dir");
		GraphicUtilsFactory.setLobbyTextFieldParameters(this.radioButtonPanel, "Program Type");
	}

	@Override	
	public void okFunction()
	{
		Configuration.setProgramExecutingName(this.programName, this.programExecutingName.getText());
		Configuration.setProgramDir(this.programName, this.programDir.getText());
		
		if(this.executable.getModel().isSelected())
			Configuration.setProgramType(this.programName, DBParameters.EXECUTABLE);
		else if(this.java.getModel().isSelected())
			Configuration.setProgramType(this.programName, DBParameters.JAVA);

		super.dispose();
		MainFrame.getInstance().showMessage("Server Configured!", JOptionPane.INFORMATION_MESSAGE);		
	}

	@Override
	public boolean checkOkFunction()
	{	
		if(!this.programExecutingName.isChecked() || !this.programDir.isChecked())
		{
			JOptionPane.showMessageDialog(this, "Please check you have filled correctly all fields!", "Error configuring the server!", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		if(!this.executable.getModel().isSelected() && !this.java.getModel().isSelected())
		{
			JOptionPane.showMessageDialog(this, "Please check you have filled correctly all fields!", "Error configuring the server!", JOptionPane.ERROR_MESSAGE);
			return false;
		}	
		
		return true;
	}
}